import 'package:app/model/app_config.dart';
import 'package:app/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class TimeBox extends StatefulWidget {
  final String name;
  final int targetTimestamp;
  final List<TDSwipeCellAction>? children;
  final VoidCallback? onTap;
  final Color color;
  const TimeBox({
    super.key,
    this.name = "",
    this.targetTimestamp = 0,
    this.children,
    this.onTap,
    this.color = StaticAppConfig.defaultEventColor,
  });
  static void close() {
    TDSwipeCell.close("test");
  }

  @override
  State<TimeBox> createState() => _TimeBoxState();
}

class _TimeBoxState extends State<TimeBox> {
  @override
  Widget build(BuildContext context) {
    var day = Utils.calculateTimeDifference2(
      widget.targetTimestamp,
      DateTime.now().millisecondsSinceEpoch,
    );
    final now = DateTime.now();
    String computerPastName() {
      final targetDate = DateTime.fromMillisecondsSinceEpoch(
        widget.targetTimestamp,
      );
      if (targetDate.year == now.year &&
          targetDate.month == now.month &&
          targetDate.day == now.day) {
        return '就是今天';
      } else if (targetDate.isBefore(now)) {
        return '已经过去';
      } else {
        return '还有';
      }
    }

    var screenWidth = MediaQuery.of(context).size.width;
    return GestureDetector(
      onTap: widget.onTap ?? () {},
      child: SizedBox(
        height: 40,
        width: double.infinity,
        child: TDSwipeCell(
          groupTag: 'test',
          right: TDSwipeCellPanel(
            extentRatio: 180 / screenWidth,
            children: widget.children ?? [],
          ),
          cell: Row(
            children: [
              Expanded(
                flex: 3,
                child: Container(
                  decoration: BoxDecoration(color: Colors.white),
                  padding: EdgeInsets.only(left: 10),
                  alignment: Alignment.center,
                  child: Row(
                    children: [
                      Expanded(
                        child: Text(
                          widget.name,
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                        ),
                      ),
                      const SizedBox(width: 3),
                      Text(computerPastName(), maxLines: 1),
                    ],
                  ),
                ),
              ),
              Expanded(
                flex: 2,
                child: Container(
                  decoration: BoxDecoration(
                    color: widget.color.withAlpha(StaticAppConfig.colorAlpha),
                  ),
                  alignment: Alignment.center,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [Text(int.parse(day.days).abs().toString())],
                  ),
                ),
              ),
              Expanded(
                flex: 1,
                child: Container(
                  decoration: BoxDecoration(color: widget.color),
                  alignment: Alignment.center,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [const Text("天")],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
